package com.daoImpl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.CraftShow;
import com.bean.CraftStandardType;
import com.dao.CraftStandardTypeDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * CraftStandardType entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.CraftStandardType
 * @author MyEclipse Persistence Tools
 */
public class CraftStandardTypeDaoImpl extends HibernateDaoSupport implements CraftStandardTypeDao {
	private static final Logger log = LoggerFactory
			.getLogger(CraftStandardTypeDaoImpl.class);
	// property constants
	public static final String CRAFT_STD_CODE = "craftStdCode";
	public static final String CRAFT_STD_NAME = "craftStdName";
	public static final String DESCRIPTION = "description";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#save(com.bean.CraftStandardType)
	 */
	@Override
	public void save(CraftStandardType transientInstance) {
		log.debug("saving CraftStandardType instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#delete(com.bean.CraftStandardType)
	 */
	@Override
	public void delete(CraftStandardType persistentInstance) {
		log.debug("deleting CraftStandardType instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findById(java.lang.Integer)
	 */
	@Override
	public CraftStandardType findById(java.lang.Integer id) {
		log.debug("getting CraftStandardType instance with id: " + id);
		try {
			CraftStandardType instance = (CraftStandardType) getHibernateTemplate()
					.get("com.bean.CraftStandardType", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findByExample(com.bean.CraftStandardType)
	 */
	@Override
	public List findByExample(CraftStandardType instance) {
		log.debug("finding CraftStandardType instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CraftStandardType instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CraftStandardType as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findByCraftStdCode(java.lang.Object)
	 */
	@Override
	public List findByCraftStdCode(Object craftStdCode) {
		return findByProperty(CRAFT_STD_CODE, craftStdCode);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findByCraftStdName(java.lang.Object)
	 */
	@Override
	public List findByCraftStdName(Object craftStdName) {
		return findByProperty(CRAFT_STD_NAME, craftStdName);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findByDescription(java.lang.Object)
	 */
	@Override
	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CraftStandardType instances");
		try {
			String queryString = "from CraftStandardType";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#merge(com.bean.CraftStandardType)
	 */
	@Override
	public CraftStandardType merge(CraftStandardType detachedInstance) {
		log.debug("merging CraftStandardType instance");
		try {
			CraftStandardType result = (CraftStandardType) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#attachDirty(com.bean.CraftStandardType)
	 */
	@Override
	public void attachDirty(CraftStandardType instance) {
		log.debug("attaching dirty CraftStandardType instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftStandardTypeDao#attachClean(com.bean.CraftStandardType)
	 */
	@Override
	public void attachClean(CraftStandardType instance) {
		log.debug("attaching clean CraftStandardType instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 
	 */
	public List<CraftStandardType> getFindCraftStandardType(String table,String pk,String sort,int PageNumber,int PageSize ,String options){
		Session session = getSession();  
		SQLQuery query=null;
		
		StringBuffer sqlbuf = new StringBuffer("execute [dbo].[sp_get_page_of_table2] ");
		sqlbuf.append("'"+table+"','"+pk+"','"+sort+"',"+PageNumber+","+PageSize+",'*',\""+options+"\"");
		
		query=session.createSQLQuery(sqlbuf.toString());
		query.addEntity(CraftStandardType.class);
		
		//获取集合
		List<CraftStandardType> list = query.list();
		
		return list;
	}
	
	public static CraftStandardTypeDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (CraftStandardTypeDao) ctx.getBean("CraftStandardTypeDaoImpl");
	}
}